using System;
using System.Threading;
using System.Threading.Tasks;
using RTMP.SimpleClient.Helpers;

namespace RTMP.SimpleClient.MessageReceiver
{
    /// <summary>
    /// RTMP消息接收器接口
    /// </summary>
    public interface IMessageReceiver : IDisposable
    {
        /// <summary>
        /// 启动后台消息接收
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>接收任务</returns>
        Task StartReceivingAsync(CancellationToken cancellationToken = default);

        /// <summary>
        /// 停止消息接收
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>停止任务</returns>
        Task StopReceivingAsync(CancellationToken cancellationToken = default);

        /// <summary>
        /// 消息接收事件
        /// </summary>
        event EventHandler<MessageReceivedEventArgs>? MessageReceived;

        /// <summary>
        /// 接收器状态
        /// </summary>
        bool IsReceiving { get; }
    }

    /// <summary>
    /// 消息接收事件参数
    /// </summary>
    public class MessageReceivedEventArgs : EventArgs
    {
        /// <summary>
        /// 消息头信息
        /// </summary>
        public RTMPMessageHelper.RTMPMessageHeader Header { get; set; }

        /// <summary>
        /// 消息负载数据
        /// </summary>
        public byte[] Payload { get; set; }

        /// <summary>
        /// 接收时间戳
        /// </summary>
        public DateTime ReceivedAt { get; set; }

        public MessageReceivedEventArgs(RTMPMessageHelper.RTMPMessageHeader header, byte[] payload)
        {
            Header = header;
            Payload = payload;
            ReceivedAt = DateTime.UtcNow;
        }
    }
}